package com.ghp.common.utils;

import java.util.Random;

/**
 * @author ghp
 * @title 用户名生成工具类
 * @description
 */
public class UserNameGeneratorUtils {

    private static final String LETTERS_NUMBERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    private static final String USER_NAME_PREFIX = "user_";

    private static final int DEFAULT_LENGTH = 6;

    /**
     * 默认生成用户名 6位字母和数组
     *
     * @return
     */
    public static String generateUsername() {
        return generateUsername(USER_NAME_PREFIX, DEFAULT_LENGTH);
    }

    /**
     * 随机生成用户名，长度自己指定
     *
     * @param length 用户名长度
     * @return
     */
    public static String generateUsername(int length) {
        return generateUsername(USER_NAME_PREFIX, DEFAULT_LENGTH);
    }

    /**
     * 随机生成用户名，长度自己指定
     *
     * @param length 用户名长度
     * @param prefix 用户名前缀
     * @return
     */
    public static String generateUsername(String prefix, int length) {
        char[] characters = LETTERS_NUMBERS.toCharArray();
        StringBuilder username = new StringBuilder(prefix);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            char c = characters[random.nextInt(characters.length)];
            username.append(c);
        }
        return username.toString();
    }


    /**
     * 用于测试，无实际作用，可删除
     */
    public static void main(String[] args) {
        System.out.println(generateUsername());
    }
}
